home *** CD-ROM | disk | FTP | other *** search
- /*
-
- Sample Patching Code for TrapWeaver
-
- Version 1 - Written by Josh Freeman
-
- (c)1998 Twilight Edge Software
-
- Email: twilight_edge@hotmail.com
-
- This file may be freely distributed provided it is unchanged.
-
- **********************************************************************
-
- This file contains four sample functions which show how to check if TrapWeaving is engaged,
- acquire the TrapWeaver suite, and set up & destroy a system patch.
-
- The TrapWeaver suite is NOT necessary in order to make use of TrapWeaver's patch management
- functionality. All programs that call SysSetTrapAddress() will be protected from conflicting
- with each other when TrapWeaving is turned on - resetting a trap to its previous address will
- not affect patches that were placed after yours.
-
- Error checking has been reduced in order to make the code easier to read.
-
- */
-
- #include <Pilot.h>
-
- #include "tw_suite.h"
-
- /**********************************************************************/
-
- #define kCreatorID '????' // Your app's creator ID here
-
- // kpatchTimGetSecondsCodeSize is the size of the patch (rounded up to nearest 10),
- // obtained by disassembling the source file. (In Codewarrior, select 'Disassemble' from
- // the Project menu with a source file as the active window)
- #define kpatchTimGetSecondsCodeSize 70
-
- /**********************************************************************/
-
- // This function checks to see if TrapWeaver is installed, and returns a pointer to the suite
- // Pass NULL for 'suiteHandle' if you don't need the suite returned
- Boolean IsTrapWeavingOn(TrapWeaverSuite1 **suiteHandle);
-
- // This function installs the patch
- Err SetupMyPatch(void);
-
- // This function removes the patch
- Err RemoveMyPatch(void);
-
- // This is the patch for TimGetSeconds()
- ULong patchTimGetSeconds(void);
-
- /**********************************************************************/
-
- Boolean IsTrapWeavingOn(TrapWeaverSuite1 **suiteHandle)
- {
- Err error;
- TrapWeaverSuite1 *twSuite;
-
- error = FtrGet(kTrapWeaverCreatorID, kTrapWeaverSuiteFtr, (DWordPtr) (&twSuite));
-
- if (error != 0)
- return false;
-
- if (suiteHandle != NULL)
- *suiteHandle = twSuite;
-
- return true;
- }
-
- /**********************************************************************/
-
- Err SetupMyPatch(void)
- {
- Err error = 0;
- TrapWeaverSuite1 *twSuite;
- VoidPtr oldTrap;
-
- // Return if TrapWeaving is not engaged
- if (!IsTrapWeavingOn(&twSuite))
- return -1;
-
- // Set up patch
- oldTrap = SysGetTrapAddress(sysTrapTimGetSeconds);
-
- FtrSet(kCreatorID, sysTrapTimGetSeconds, (DWord) oldTrap);
-
- /*
- The suite function, SetupPatch() makes patching easier by copying your code to a safe,
- locked location & changing the trap address all in one function call. Your patches no longer
- have to be placed in a separate resource or source file, manually locked, then patched.
- The tradeoff is that you will have to keep track of the size of the patch, which can be
- found by disassembling the source file.
- */
-
- error = twSuite->SetupPatch(sysTrapTimGetSeconds, patchTimGetSeconds,
- kpatchTimGetSecondsCodeSize);
-
- return error;
- }
-
- /**********************************************************************/
-
- Err RemoveMyPatch(void)
- {
- Err error;
- VoidPtr oldTrap;
-
- // Get old trap address
- error = FtrGet(kCreatorID, sysTrapTimGetSeconds, (DWordPtr) &oldTrap);
-
- // Restore the trap to the old address
- if (error == 0)
- {
- SysSetTrapAddress(sysTrapTimGetSeconds, oldTrap);
- FtrUnregister(kCreatorID, sysTrapTimGetSeconds);
- }
-
- return error;
- }
-
- /**********************************************************************/
-
- ULong patchTimGetSeconds(void)
- {
- ULong (*oldTrap) (void);
- ULong returnValue;
-
- // Do stuff here...
-
- // Call old trap
- FtrGet(kCreatorID, sysTrapTimGetSeconds, (DWordPtr) &oldTrap);
- returnValue = oldTrap();
-
- // ...or do stuff here
-
- return returnValue;
- }
-